﻿using System;
using System.Collections.Specialized;
using System.Configuration;
using System.Configuration.Provider;
using System.Threading;
using System.Xml;

namespace FileMonitor.Service.Configuration
{
    internal static class ProvidersHelper
    {

        public static ProviderBase InstantiateProvider(ProviderSettings providerSettings, Type providerType)
        {
            ProviderBase base2 = null;
            try
            {
                string str = (providerSettings.Type == null) ? null : providerSettings.Type.Trim();
                if (string.IsNullOrEmpty(str))
                {
                    throw new ArgumentException("Provider_no_type_name");
                }
                Type c = GetType(str, "type", providerSettings, null, true);
                if (!providerType.IsAssignableFrom(c))
                {
                    throw new ArgumentException("Provider_must_implement_type:" + providerType.ToString());
                }
                base2 = (ProviderBase)Activator.CreateInstance(c);
                NameValueCollection parameters = providerSettings.Parameters;
                NameValueCollection config = new NameValueCollection(parameters.Count, StringComparer.Ordinal);
                foreach (string str2 in parameters)
                {
                    config[str2] = parameters[str2];
                }
                base2.Initialize(providerSettings.Name, config);
            }
            catch (Exception exception)
            {
                if (exception is ConfigurationException)
                {
                    throw;
                }
                throw new ConfigurationErrorsException(exception.Message, providerSettings.ElementInformation.Properties["type"].Source, providerSettings.ElementInformation.Properties["type"].LineNumber);
            }
            return base2;
        }
        public static void InstantiateProviders(ProviderSettingsCollection configProviders, ProviderCollection providers, Type providerType)
        {
            foreach (ProviderSettings settings in configProviders)
            {
                providers.Add(InstantiateProvider(settings, providerType));
            }
        }

        internal static Type GetType(string typeName, string propertyName, ConfigurationElement configElement, XmlNode node, bool ignoreCase)
        {
            Type type;
            try
            {
                type = Type.GetType(typeName, true, ignoreCase);
            }
            catch (Exception exception)
            {
                if (((exception is ThreadAbortException) || (exception is StackOverflowException)) || (exception is OutOfMemoryException))
                {
                    throw;
                }
                if (node != null)
                {
                    throw new ConfigurationErrorsException(exception.Message, exception, node);
                }
                if (configElement != null)
                {
                    throw new ConfigurationErrorsException(exception.Message, exception, configElement.ElementInformation.Properties[propertyName].Source, configElement.ElementInformation.Properties[propertyName].LineNumber);
                }
                throw new ConfigurationErrorsException(exception.Message, exception);
            }

            return type;
        }




    }
}
